package com.lqs.sofp.servlet;

import com.lqs.sofp.entity.Manager;
import com.lqs.sofp.service.ManagerService;
import com.lqs.sofp.service.impl.ManagerServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/**
 * @author lkh
 */
@WebServlet(name = "ManagerLoginServlet", value = "/Manager/mls")
public class ManagerLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.收参完毕
        String manager = request.getParameter("manager");
        String password = request.getParameter("password");
        //2.调用服务进行查找
        ManagerService managerInfo = new ManagerServiceImpl();
        Manager admin = managerInfo.login(manager,password);
        //3.业务逻辑判断
        if (admin != null){
            //说明登录成功。
            //存储在session作用域
            HttpSession session = request.getSession();
            session.setAttribute("Manager", admin);
            //将业务转发到另一个用来执行进入社区页面的servlet
            RequestDispatcher dispatcher =  request.getRequestDispatcher("/html/backstage");
            dispatcher.forward(request, response);
        }else{
            //登录不成功重定向到登录页面。
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
